<template>
    <div class="postsection" style="">
        <div style="margin-bottom: 10px">
            <el-button @click="add" type="primary" size="mini" style="margin-left: 8px;">新增</el-button>
        </div>
        <el-table :data="tableData.filter(data => !search || data.name.toLowerCase().includes(search.toLowerCase()))"
            style="width: 100%" border>
            <el-table-column label="id" prop="id">
            </el-table-column>
            <el-table-column label="分区名字" prop="typeName">
            </el-table-column>
            <el-table-column label="分区代码" prop="typeCode">
            </el-table-column>
            <el-table-column label="创建时间" prop="createTime">
            </el-table-column>

            <el-table-column align="center" label="操作" width="160">
                <template slot-scope="scope">
                    <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">修改</el-button>
                    <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <!-- 弹窗   -->
        <el-dialog title="分区" :visible.sync="dialogFormVisible" width="30%" :close-on-click-modal="false">
            <el-form :model="entity">
                <el-form-item label="分区名字" label-width="150px">
                    <el-input v-model="entity.typeName" autocomplete="off" style="width: 80%"></el-input>
                </el-form-item>
                <el-form-item label="分区代码" label-width="150px">
                    <el-input v-model="entity.typeCode" autocomplete="off" style="width: 80%"></el-input>
                </el-form-item>

            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible = false">取 消</el-button>
                <el-button type="primary" @click="save()">确 定</el-button>
            </div>
        </el-dialog>
        <!-- 分页 -->
        <div class="page" style="margin: 24px auto;display: flex;justify-content: center;" v-show="pageShow">
            <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
                :current-page.sync="currentPage" :page-size="pageSize" :page-sizes="[3, 6, 9, 12]"
                layout="total, prev, pager, next" :total="total">
            </el-pagination>
        </div>
    </div>
</template>

<script>
import API from "@/api/request";
export default {
    inject: ['reload'],
    data() {
        return {
            tableData: [],
            search: '',
            //数据总数
            total: 0,
            //当前页
            currentPage: 1,
            //每页数量
            pageSize: 3,
            //控制分页显示
            pageShow: false,
            dialogFormVisible: false,
            entity: {},
        }
    },
    methods: {

        handleEdit(index, row) {
            console.log(index, row);
            this.entity = JSON.parse(JSON.stringify(row))
            // this.userCommentDto=JSON.parse(JSON.stringify(this.entity.userCommentDto))
            // console.log(this.userCommentDto)
            this.dialogFormVisible = true;
        },
        handleDelete(index, row) {
            console.log(row)
            API.delete("/api/postSection/" + row.id).then(res => {
                if (res.data.code === 200) {
                    this.$message.success("操作成功!");
                } else {
                    this.$message.error("操作失败!");
                    console.error("API 请求失败", res);
                }
                this.reload();
            }).catch(error => {
                console.error("API 请求错误", error);
            });
        },

        getpostsections() {
            API.get("/api/postSection/pageList", {
                params: {
                    pageNum: this.currentPage,
                    pageSize: this.pageSize
                }
            }).then(res => {
                if (res.data.code === 200) {
                    this.tableData = res.data.data.records;
                    this.total = res.data.data.total;
                    this.currentPage = res.data.data.current;
                    if (this.total > 0) {
                        this.pageShow = true;
                    }

                } else {
                    console.error("API 请求失败", res);
                }
            }).catch(error => {
                console.error("API 请求错误", error);
            });
        },
        //控制页数的函数
        handleSizeChange(val) {
            this.pageSize = val;
            console.log(`每页 ${val} 条`);
            this.getpostsections();

        },
        // 控制页码的函数
        handleCurrentChange(val) {
            this.currentPage = val;
            console.log("查看当前页" + this.currentPage);
            console.log(`当前 ${val} 页`);
            this.getpostsections();
        },
        save() {
            if (!this.entity.id) {
                //新增
                API.post("/api/postSection/add", {
                    "id": this.entity.id,
                    "typeName": this.entity.typeName,
                    "typeCode": this.entity.typeCode,
                }).then(res => {
                    if (res.data.code === 200) {
                        this.$message.success("操作成功!");
                        this.dialogFormVisible = false;
                        this.reload()
                    } else {
                        console.error("API 请求失败", res);
                    }
                }).catch(error => {
                    console.error("API 请求错误", error);
                });
            } else {
                API.put("/api/postSection/update", {
                    "id": this.entity.id,
                    "typeName": this.entity.typeName,
                    "typeCode": this.entity.typeCode,
                }).then(res => {
                    if (res.data.code === 200) {
                        this.$message.success("操作成功!");
                        this.dialogFormVisible = false;
                        this.reload()
                    } else {
                        this.$message.error("操作失败!");
                        console.error("API 请求失败", res);
                    }
                }).catch(error => {
                    console.error("API 请求错误", error);
                });
            }

        },
        add() {
            this.dialogFormVisible = true;
            //清空对象
            this.entity = {}
            console.log("这是对象")
            console.log(JSON.stringify(this.entity))
        },


    },


    created() {
        this.getpostsections();
    }
}
</script>
<style>
.postsection {
    padding: 20px;
}
</style>